package com.cj.sys.web.security;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cj.common.orm.hibernate.EntityManager;
import com.cj.common.web.struts2.CRUDActionSupport;
import com.cj.common.web.struts2.Struts2Utils;
import com.cj.sys.entity.security.Dept;
import com.cj.sys.entity.security.Resource;
import com.cj.sys.service.security.DeptManager;

public class DeptAction extends CRUDActionSupport<Dept> {
	@Autowired
	private DeptManager deptManager;

	private Dept oldParent;
	private final Long rootId = 1L;
	private Long exceptionId;
	@Override
	public EntityManager getEntityManager() {
		return deptManager;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (getId() != null) {
			entity = deptManager.get(getId());
		} else {
			entity = new Dept();
		}
	}
	
	@Override
	public void prepareSave() throws Exception { // 用prepareModel修改时会出错
		if (getId() != null) {
			entity = deptManager.get(getId());
			oldParent = entity.getParent();
			entity.setParent(null);
		} else {
			entity = new Dept();
		}
	}

	public void deptTree(){
		String xmlDoc;
		if(getId() == null)
			setId(rootId);
		xmlDoc = deptManager.createDeptsXml(getId(),exceptionId);
		Struts2Utils.renderXml(xmlDoc);
	}
	
	public List<Dept> getAllDepts() {
		return deptManager.getAll();
	}

	public void setExceptionId(Long exceptionId) {
		this.exceptionId = exceptionId;
	}
	
}
